﻿var tempPath;
var groupid;
var uploadstatus;
var avatarselection;
var x1 = 10;
var y1 = 10;
var x2 = 200;
var y2 = 200;



function FileUploadComplete(sender, e) {
    var ext = e._fileName.getFileExt();
    var imgPath = tempPath + "g."+ groupid + "." + ext + "?" + (new Date());
    $('.previewimg').attr("src", imgPath);
    $('#avatar-image-select').attr("src", imgPath);
    showlightbox();
//    $('a[rel=lightbox]')[0].href = imgPath;
//    $('a[rel=lightbox]').lightBox({ fixedNavigation: true,
//        imageBtnClose: '../../Content/images/icons/oktick.png',
//        imageLoading: '../../Content/images/icons/ajax-loader-w.gif'

//    });
//    $('a[rel=lightbox]').click(showlightbox);
//    $('a[rel=lightbox]').click();
//    $('#lightbox-image-details-caption')[0].innerHTML = $('#submitbox')[0].innerHTML;
}

function showlightbox() {
    avatarselection = $('#avatar-image-select').imgAreaSelect({ aspectRatio: '1:1', x1: x1, y1: y1, x2: x2, y2: y2,
        handles: true,
        parent: '#imageselectdialog',
        minWidth: 150, minHeight: 150,
        onSelectChange: preview
    });

    
    $('div[title=hiddens] input[type=hidden]')[0].value = x1 + ":" +y1 + ":" + x2 + ":" + y2;

    $("#imageselectdialog").dialog('open');
    
    /*
    if ($('#lightbox-image').is(':visible')) {
        avatarselection = $('#lightbox-image').imgAreaSelect({ aspectRatio: '1:1', x1: x1, y1: y1, x2: x2, y2: y2, handles: true, parent: '#jquery-lightbox',
            minWidth: 150, minHeight: 150, onSelectChange: preview
        });
        $('#jquery-lightbox').unbind('click');
        $('#lightbox-nav').remove();

        $('#lightbox-secNav-btnClose img').bind('click', function() {
            $('#editbuttonbox').css('display', '');
        });

        $('#jquery-overlay').click(function() { return false; });
    }
    else {
        setTimeout(showlightbox, 50);
    }
    */
}

function FileUploadStarted(sender, e) {
    $('#editbuttonbox').hide();
    var ext = e.get_fileName().getFileExt();
    if (ext == 'jpg' || ext == 'jpeg' || ext == 'gif' || ext == 'png' || ext == 'bmp') {
        return true;
    }
    else {
        e.isvalid = false;
        e.set_cancel(true);
        e.set_errorMessage('"' + e.get_fileName() + '"' + " 不是图像文件。\n必须使用图像文件，例如：jpg, png");
        return false;
    }

}

function FileUploadError(sender, args) {
    alert(args.get_errorMessage());
}

function preview(img, selection) {
    if (!selection.width || !selection.height) {
        avatarselection = $('#avatar-image-select').imgAreaSelect({ aspectRatio: '1:1', x1: x1, y1: y1, x2: x2, y2: y2, handles: true, parent: '#imageselectdialog',
            minWidth: 150, minHeight: 150, onSelectChange: preview
        });
        return;
    }

    var scaleX = 150 / selection.width;
    var scaleY = 150 / selection.height;

    $('.previewimg').css({
        width: Math.round(scaleX * img.width),
        height: Math.round(scaleY * img.height),
        left: -Math.round(scaleX * selection.x1),
        top: -Math.round(scaleY * selection.y1)
    });

    $('div[title=hiddens] input[type=hidden]')[0].value = selection.x1 + ":" + selection.y1 + ":" + selection.x2 + ":" + selection.y2;
    x1 = selection.x1;
    x2 = selection.x2;
    y1 = selection.y1;
    y2 = selection.y2;
}

$(function () {
    tempPath = relRootPath + "/Content/AvatarImages/Temp/";
    $('#editbuttonbox').hide();
    $('#modifyimageselectionbutton').click(ModifyImageSelection);
    //    $('.imageselectpanel').css('display', 'none');
    //$('#previewbox').imageFrame();

    dlg = $("#imageselectdialog").dialog({
        autoOpen: false,
        height: 700,
        width: 810,
        zIndex: 900,
        dragable: false,
        resizable: false,
        modal: true,
        close: function (event, ui) { $('#editbuttonbox').show(); },
        open: function (event, ui) {
            $(this).css('height', 'auto');
        }
    });

    $('#closepreviewdialogbutton').click(function () {
        $("#imageselectdialog").dialog('close');
    });

    //$('.previewbox').dropShadow();
    //$('.previewbox').redrawShadow();
    //$('.normal input[type=file]').css('visibility', 'hidden');
});


function ModifyImageSelection(sender, args) {
    showlightbox({left:2,right:2});
}

function PopupFileUpload() {$('.normal input[type=file]').click(); }